package br.ufmg.vod.collections;

import java.util.Collection;
import java.util.Iterator;

public class XRangeCollection implements Collection<Integer> {
	
	private final int end;
	private final int start;
	
	public XRangeCollection(int end){
		this(0,end);
	}

	public XRangeCollection(int start, int end){
		this.start = start;
		this.end = end;
	}

	@Override
	public boolean add(Integer e) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean addAll(Collection<? extends Integer> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void clear() {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean contains(Object o) {
		Integer i = (Integer) o;
		return i >= start && i < end;
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean isEmpty() {
		return start >= end;
	}

	@Override
	public Iterator<Integer> iterator() {
		return new XRange(start,end);
	}

	@Override
	public boolean remove(Object o) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		throw new UnsupportedOperationException();
	}

	@Override
	public int size() {
		return end-start;
	}

	@Override
	public Object[] toArray() {
		Integer[] a = new Integer[size()];
		for (int i = start; i < end; i++) {
			a[i]=i;
		}
		return a;
	}

	@Override
	public <T> T[] toArray(T[] a) {
		throw new UnsupportedOperationException();
	}


}
